package com.ruoyi.sxt.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SxtJuese;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.sxt.domain.SxtYonghu;
import com.ruoyi.sxt.domain.SxtYonghuGangwei;
import com.ruoyi.sxt.domain.SxtYonghuJuese;
import com.ruoyi.sxt.mapper.SxtJueseMapper;
import com.ruoyi.sxt.mapper.SxtYonghuGangweiMapper;
import com.ruoyi.sxt.mapper.SxtYonghuJueseMapper;
import com.ruoyi.sxt.mapper.SxtYonghuMapper;
import com.ruoyi.sxt.service.ISxtYonghuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 用户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-12
 */
@Service
public class SxtYonghuServiceImpl implements ISxtYonghuService
{
    @Autowired
    private SxtYonghuMapper sxtYonghuMapper;

    @Autowired
    private SxtYonghuGangweiMapper sxtYonghuGangweiMapper;

    @Autowired
    private SxtYonghuJueseMapper sxtYonghuJueseMapper;

    @Autowired
    private SxtJueseMapper sxtJueseMapper;


    /**
     * 查询用户信息
     * 
     * @param userId 用户信息主键
     * @return 用户信息
     */
    @Override
    public SxtYonghu selectSxtYonghuByUserId(Long userId)
    {
        return sxtYonghuMapper.selectSxtYonghuByUserId(userId);
    }

    /**
     * 查询用户信息列表
     * 
     * @param sxtYonghu 用户信息
     * @return 用户信息
     */
    @Override
    public List<SxtYonghu> selectSxtYonghuList(SxtYonghu sxtYonghu)
    {
        return sxtYonghuMapper.selectSxtYonghuList(sxtYonghu);
    }

    @Override
    public List<Long> selectSxtYonghuListByGangweiId(Long userId) {
        return sxtYonghuMapper.selectSxtYonghuListByGangweiId(userId);
    }

    @Override
    public List<Long> selectSxtYonghuListByJueseId(Long userId) {
        return sxtYonghuMapper.selectSxtYonghuListByJueseId(userId);
    }

    @Override
    public List<SxtJuese> selectSxtJueseList(Long userId) {
        SxtJuese juese = new SxtJuese();
        List<SxtJuese> jueseList = sxtJueseMapper.selectSxtJueseList(juese);
        List<Long> roleIds = sxtYonghuMapper.selectSxtYonghuListByJueseId(userId);
        if(jueseList != null && jueseList.size() > 0){
            for(SxtJuese sxtJuese : jueseList){
                if(roleIds != null && roleIds.size() > 0){
                    for(Long roleId : roleIds){
                        if(roleId == sxtJuese.getRoleId()){
                            sxtJuese.setJudge(true);
                            break;
                        }
                    }
                }else {
                    break;
                }
            }
        }
        return jueseList;
    }

    @Override
    @Transactional
    public void insertAuthJuese(Long userId, Long[] rolesIds) {
        sxtYonghuJueseMapper.deleteSxtYonghuJueseByUserId(userId);
        SxtYonghu sxtYonghu = new SxtYonghu();
        sxtYonghu.setUserId(userId);
        sxtYonghu.setRoleIds(rolesIds);
        insertYonghuJuese(sxtYonghu);
    }

    /**
     * 新增用户信息
     * 
     * @param sxtYonghu 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSxtYonghu(SxtYonghu sxtYonghu)
    {
        sxtYonghu.setCreateTime(DateUtils.getNowDate());
        int rows = sxtYonghuMapper.insertSxtYonghu(sxtYonghu);
        insertYonghuGangwei(sxtYonghu);
        insertYonghuJuese(sxtYonghu);
        return rows;
    }

    @Override
    public int resetPwd(SxtYonghu sxtYonghu) {
        return sxtYonghuMapper.updateSxtYonghu(sxtYonghu);
    }

    public void insertYonghuGangwei(SxtYonghu yonghu){
        Long[] postIds = yonghu.getPostIds();
        if(postIds != null && postIds.length > 0){
            List<SxtYonghuGangwei> yonghuGangweiList = new ArrayList<>();
            for(Long postId : postIds){
                SxtYonghuGangwei yonghuGangwei = new SxtYonghuGangwei();
                yonghuGangwei.setUserId(yonghu.getUserId());
                yonghuGangwei.setPostId(postId);
                yonghuGangweiList.add(yonghuGangwei);
            }
            sxtYonghuGangweiMapper.batchYonghuGangwei(yonghuGangweiList);
        }
    }

    public void insertYonghuJuese(SxtYonghu yonghu){
        Long[] roleIds = yonghu.getRoleIds();
        if(roleIds != null && roleIds.length > 0){
            List<SxtYonghuJuese> yonghuJueseList = new ArrayList<>();
            for(Long roleId : roleIds){
                SxtYonghuJuese yonghuJuese = new SxtYonghuJuese();
                yonghuJuese.setUserId(yonghu.getUserId());
                yonghuJuese.setRoleId(roleId);
                yonghuJueseList.add(yonghuJuese);
            }
            sxtYonghuJueseMapper.batchYonghuJuese(yonghuJueseList);
        }
    }

    /**
     * 修改用户信息
     * 
     * @param sxtYonghu 用户信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSxtYonghu(SxtYonghu sxtYonghu)
    {
        sxtYonghu.setUpdateTime(DateUtils.getNowDate());
        Long userId = sxtYonghu.getUserId();
        sxtYonghuGangweiMapper.deleteSxtYonghuGangweiByUserId(userId);
        insertYonghuGangwei(sxtYonghu);
        sxtYonghuJueseMapper.deleteSxtYonghuJueseByUserId(userId);
        insertYonghuJuese(sxtYonghu);
        return sxtYonghuMapper.updateSxtYonghu(sxtYonghu);
    }

    /**
     * 批量删除用户信息
     * 
     * @param userIds 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSxtYonghuByUserIds(Long[] userIds)
    {
        sxtYonghuGangweiMapper.deleteSxtYonghuGangweiByUserIds(userIds);
        sxtYonghuJueseMapper.deleteSxtYonghuJueseByUserIds(userIds);
        return sxtYonghuMapper.deleteSxtYonghuByIds(userIds);
    }

    /**
     * 删除用户信息信息
     * 
     * @param userId 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteSxtYonghuByUserId(Long userId)
    {
        return sxtYonghuMapper.deleteSxtYonghuByUserId(userId);
    }
}
